<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>Trie前缀树 | 我的代码片段</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="1234567891011121314151617181920212223242526272829303132333435363738394041424344class Node:    def __init__(self):        self.is_last = False        self.children = &amp;#123;&amp;#125;    def __repr__(self):">
<meta property="og:type" content="article">
<meta property="og:title" content="Trie前缀树">
<meta property="og:url" content="https://yixian.gitee.io/personal_site/2018/07/02/Trie前缀树/index.html">
<meta property="og:site_name" content="我的代码片段">
<meta property="og:description" content="1234567891011121314151617181920212223242526272829303132333435363738394041424344class Node:    def __init__(self):        self.is_last = False        self.children = &amp;#123;&amp;#125;    def __repr__(self):">
<meta property="og:locale" content="zh">
<meta property="og:updated_time" content="2018-07-02T01:57:24.321Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Trie前缀树">
<meta name="twitter:description" content="1234567891011121314151617181920212223242526272829303132333435363738394041424344class Node:    def __init__(self):        self.is_last = False        self.children = &amp;#123;&amp;#125;    def __repr__(self):">
  
    <link rel="alternate" href="/atom.xml" title="我的代码片段" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/css/style.css">
</head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">我的代码片段</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://yixian.gitee.io/personal_site"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-Trie前缀树" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/07/02/Trie前缀树/" class="article-date">
  <time datetime="2018-07-02T01:56:49.000Z" itemprop="datePublished">2018-07-02</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Trie前缀树
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Node</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self)</span>:</span></span><br><span class="line">        self.is_last = <span class="keyword">False</span></span><br><span class="line">        self.children = &#123;&#125;</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__repr__</span><span class="params">(self)</span>:</span></span><br><span class="line">        <span class="keyword">return</span> <span class="string">'Node(&#123;&#125;,[&#123;&#125;])'</span>.format(self.is_last, <span class="string">','</span>.join(<span class="string">'&#123;&#125;:&#123;&#125;'</span>.format(x, repr(self.children[x])) <span class="keyword">for</span> x <span class="keyword">in</span> self.children))</span><br><span class="line"></span><br><span class="line">        </span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Trie</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self)</span>:</span></span><br><span class="line">        self.root = Node()</span><br><span class="line">        </span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">generate_node</span><span class="params">(self, word:str)</span> -&gt; Node:</span></span><br><span class="line">        node = Node()</span><br><span class="line">        <span class="keyword">if</span> len(word) &gt; <span class="number">0</span>:</span><br><span class="line">            node.children[word[<span class="number">0</span>]] = self.generate_node(word[<span class="number">1</span>:])</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            node.is_last = <span class="keyword">True</span></span><br><span class="line">        <span class="keyword">return</span> node</span><br><span class="line">        </span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">get_node_rest</span><span class="params">(self, root:Node, word:str)</span>-&gt; tuple:</span></span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">not</span> word:</span><br><span class="line">            <span class="keyword">return</span> root, <span class="string">''</span></span><br><span class="line">        <span class="keyword">elif</span> word[<span class="number">0</span>] <span class="keyword">in</span> root.children:</span><br><span class="line">            <span class="keyword">return</span> self.get_node_rest(root.children[word[<span class="number">0</span>]], word[<span class="number">1</span>:])</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="keyword">return</span> root, word</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">insert</span><span class="params">(self, word)</span>:</span></span><br><span class="line">        node, rest = self.get_node_rest(self.root, word)</span><br><span class="line">        <span class="keyword">if</span> rest:</span><br><span class="line">            node.children[rest[<span class="number">0</span>]] = self.generate_node(rest[<span class="number">1</span>:])</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            node.is_last = <span class="keyword">True</span></span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">search</span><span class="params">(self, word)</span>:</span></span><br><span class="line">        node, rest = self.get_node_rest(self.root, word)</span><br><span class="line">        <span class="keyword">return</span> rest == <span class="string">''</span> <span class="keyword">and</span> node.is_last</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">startsWith</span><span class="params">(self, word)</span>:</span></span><br><span class="line">        _, rest = self.get_node_rest(self.root, word)</span><br><span class="line">        <span class="keyword">return</span> len(rest) == <span class="number">0</span></span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://yixian.gitee.io/personal_site/2018/07/02/Trie前缀树/" data-id="cjj3mgml60001kkueqbi1jy11" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
  
    <a href="/2018/07/02/flatten/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">flatten</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/07/">July 2018</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2018/07/02/Trie前缀树/">Trie前缀树</a>
          </li>
        
          <li>
            <a href="/2018/07/02/flatten/">flatten</a>
          </li>
        
          <li>
            <a href="/2018/07/02/hello-world/">Hello World</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2018 杜逸先<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>



  </div>
</body>
</html>